const db = require('../utils/db.js')

// getList 获取班级列表
const getAllList = (req, res) => {
  const sql = `select c.id,c.className,c.t_id,u.name as teacherName,c.is_delete from t_class as c join t_users as u where c.t_id=u.id`
  db.query(sql, (err, results) => {
    console.log(results);
    if (err) return res.cc(err)
    res.ok(results, "获取列表成功")
  })
}
// getList 获取班级列表
const getListisNotDel = (req, res) => {
  const sql = `select c.id,c.className,c.t_id,u.name as teacherName from t_class as c join t_users as u where c.t_id=u.id and c.is_delete=0`
  db.query(sql, (err, results) => {
    console.log(results);
    if (err) return res.cc(err)
    res.ok(results, "获取列表成功")
  })
}

// 添加班级
const addClass = (req, res) => {
  const sql = `select * from t_class where className = ?`
  db.query(sql, req.body.className, (err, results) => {
    if (err) {
      return res.cc(err);
    }
    if (results.length >= 1) {
      return res.cc("该班级名称已存在，请更换班级名称。")
    }
    const sql = `insert into t_class set ?`
    db.query(sql, req.body, (err, results) => {
      if (err) {
        return res.cc(err);
      }
      if (results.affectedRows >= 1) {
        return res.ok(null, "添加班级成功！")
      }
      res.ok(null, "添加班级失败！")
    })
  })
}

// 删除班级
const deleteClass = (req, res) => {
  // res.cc("ok")
  const sql = `update t_class set is_delete=1 where id=?`
  db.query(sql, req.params.id, (err, results) => {
    if (err) {
      return res.cc(err)
    }
    if (results.affectedRows == 1) {
      return res.ok(null, "删除班级成功")
    }
    return res.cc("删除失败")
  })
}

// 更新班级信息
const updateById = (req, res) => {
  // res.cc("ok")
  console.log(req.body);
  const sql = `update t_class set ? where id=?`
  db.query(sql, [req.body, req.body.id], (err, results) => {
    if (err) {
      return res.cc(err)
    }
    console.log(results);
    if (results.affectedRows == 1) {
      return res.ok(null, "修改班级信息成功")
    }
    return res.cc("修改失败")
  })
}

// 班级的课程操作

// 添加
const addCourse = (req, res) => {
  console.log(req.body);
  const { class_id, course_id } = req.body
  // 先查询是否存在
  const sql = `select * from t_class_course where class_id=? and course_id=?`
  db.query(sql, [class_id, course_id], (err, results) => {
    if (err) return res.cc(ok)
    if (results.length > 0) {
      return res.ok(null, "该班级的课程已存在")
    }
    const sql = `insert into t_class_course set ?`
    db.query(sql, req.body, (err, results) => {
      if (err) return res.cc(err)
      if (results.affectedRows == 1) {
        return res.ok(null, "添加成功")
      }
      return res.cc("添加失败")
    })
  })

}

// 修改  /  逻辑删除
const updateCourse = (req, res) => {
  const { class_id, course_id, is_delete, t_id } = req.body
  const sql = `update t_class_course set ? where class_id=? and course_id=?`
  db.query(sql, [{ is_delete, t_id }, class_id, course_id], (err, results) => {
    if (err) return res.cc(err)
    if (results.affectedRows == 1) {
      return res.ok(null, "修改成功")
    }
  })
}

// 课程列表
const getCourseList = (req, res) => {
  console.log(req.params);
  const sql = `select c.id as classId,c.className,cs.id as course_id,cc.is_delete,
  cs.name as courseName,
  cc.t_id,tu.name as teacherName 
  from 
  t_class as c,t_class_course as cc,
  t_users as tu,t_course as cs 
  where c.id=? and c.id = cc.class_id and cc.t_id=tu.id and 
  cs.id = cc.course_id`
  db.query(sql, req.params.id, (err, results) => {
    if (err) return res.cc(err)
    return res.ok(results, "获取列表数据成功")
  })
  // res.cc("ok")
}


// 获取未添加的课程
const getCourseNotList = (req, res) => {
  const sql = ` SELECT * FROM t_course WHERE id NOT IN (SELECT course_id FROM t_class_course AS cc WHERE cc.class_id = ?)`;
  db.query(sql, req.params.id, (err, results) => {
    if (err) return res.cc(err)
    return res.ok(results)
  })
}

module.exports = {
  addClass, deleteClass, getAllList, getListisNotDel, updateById, addCourse, updateCourse, getCourseList, getCourseNotList
}